package com.sw.study.springbootstudy.framework;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sw.study.springbootstudy.domain.PageDomain;
import com.sw.study.springbootstudy.domain.TableDataInfo;
import com.sw.study.springbootstudy.domain.TableSupport;
import com.sw.study.springbootstudy.util.SqlUtil;

import java.util.List;

/**
 * @author sw
 * @project springboot-study
 * @description 基础controller
 * @date 2020/4/22 09:35
 */
public class BaseController {

    /**
     * 设置请求分页数据
     */
    protected void startPage(){
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum()==null?1:pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize()==null?2:pageDomain.getPageSize();
        System.out.println("pageNum:"+pageNum+",pageSize:"+pageSize);
        if (null !=pageNum && null !=pageSize) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 封装分页数据
     * @param list
     * @return
     */
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
}
